﻿using System;
using System.Collections.Generic;
using UnityEngine;

class GameMap : MonoBehaviour
{
    Vector2Int size;

    [SerializeField]
    Transform ground;

    [SerializeField]
    Material[] materials;

    GameMapTile[] tiles;

    [SerializeField]
    GameMapTile tileprefab = default;

    public void Initialize(Vector2Int size)
    {
        this.size = size;
        //创建瓦片
        Vector2 offset = new Vector2((size.x - 1) * 0.5f, (size.y - 1) * 0.5f);//偏移量
        tiles = new GameMapTile[size.x * size.y];
        for (int i = 0, y = 0; y < size.y; y++)
        {
            for (int x = 0; x < size.x; x++,i++)
            {
                EnumTile tileType = EnumTile.floor;
                GameMapTile tile = tiles[i] = Instantiate(tileprefab);
                tile.Initialize(tileType, materials[(int)tileType]);
                tile.transform.SetParent(ground, false);
                tile.transform.localPosition = new Vector3(x - offset.x, 0.2f, y - offset.y);

            }
        }
    }
}
